﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Persistente.Marca
{
    public class PMarca
    {
        private DB_SIMETRIA _db;
        private SqlConnection _conexao = new SqlConnection(ConfigurationManager.ConnectionStrings["DB_SIMETRIA_SQL"].ConnectionString);
        public bool Salvar(string nmMarca, string dsMarca)
        {
            try
            {
                _db = new DB_SIMETRIA();
                var tbMarca = new tb_marca();
                tbMarca.nm_marca = nmMarca;
                tbMarca.ds_marca = dsMarca;
             
                _db.AddTotb_marca(tbMarca);
                _db.SaveChanges();

                return true;

            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                _db.Connection.Close();
            }

        }

        public bool Remover(int idMarca)
        {
            try
            {
                _db = new DB_SIMETRIA();
                var obTbMarca = _db.tb_marca.Where(q => q.id_marca == idMarca).FirstOrDefault();
                if (obTbMarca == null)
                {
                    return false;
                }
                else
                {
                    _db.tb_marca.DeleteObject(obTbMarca);
                    _db.SaveChanges();
                    return true;
                }

            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                _db.Connection.Close();
            }
        }

        public DataSet pegarMarcas(string nmMarca = null, string dsMarca = null)
        {

            var ds = new DataSet();

            string query = "SELECT ";
            query += " id_marca";
            query += ", nm_marca";
            query += ", ds_marca";
            query += " FROM tb_marca";

            query += " WHERE 1 = 1";

         
            if (!string.IsNullOrEmpty(nmMarca))
            {
                query += " AND nm_marca LIKE '%" + nmMarca + "%'";
            }if (!string.IsNullOrEmpty(dsMarca))
            {
                query += " AND ds_marca LIKE '%" + dsMarca + "%'";
            }

            var cmd = new SqlCommand(query, _conexao);

            cmd.Connection = _conexao;

            if (_conexao.State == ConnectionState.Closed)
            {
                _conexao.Open();
            }

            var dataAdapter = new SqlDataAdapter(cmd);
            dataAdapter.Fill(ds);

            _conexao.Close();

            return ds;
        }

        public tb_marca pegarMarcaPorId(int idMarca)
        {
            try
            {
                _db = new DB_SIMETRIA();
                return _db.tb_marca.Where(q => q.id_marca == idMarca).FirstOrDefault();
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                _db.Connection.Close();
            }
        }

        public bool Alterar(int idMarca, tb_marca obTbMarcaNovo)
        {
            try
            {
                _db = new DB_SIMETRIA();
                var obTbMarcaVelho =
                    _db.tb_marca.Where(q => q.id_marca == idMarca).FirstOrDefault();

                obTbMarcaVelho.nm_marca = obTbMarcaNovo.nm_marca;
                obTbMarcaVelho.ds_marca = obTbMarcaNovo.ds_marca;
                
                _db.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                _db.Connection.Close();
            }
        } 
    }
}
